<?php
require_once('Extension/Controller/Plugin/Abstract.php');

class Extension_Controller_Plugin_Web_Search_Lucene extends Extension_Controller_Plugin_Abstract
{
    //public function routeStartup(Zend_Controller_Request_Abstract $request) {}
    //public function routeShutdown(Zend_Controller_Request_Abstract $request) {}

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        self::initSearchLucene($request);
    }

    //public function preDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function postDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function dispatchLoopShutdown() {}


    public static function initSearchLucene(Zend_Controller_Request_Abstract $request)
    {
        $cache = Zend::initCache('Lucene');

        if (false != $cache) {
            $id = 'lucene_' . Zend::initLocale();
            $formSearchLucene = $cache->load($id);
            if(false == $formSearchLucene) {
                $config = Zend::initConfig('Search/Lucene');
                $formSearchLucene = new Extension_Form($config->form);
                $cache->save($formSearchLucene->render());
            }
        } else {
            $config = Zend::initConfig('Search/Lucene');
            $formSearchLucene = new Extension_Form($config->form);
        }

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $view = $viewRenderer->view;
        $view->formSearchLucene = $formSearchLucene;
    }
}